(load "~/quicklisp/setup.lisp")
(ql:quickload '(cl-who parenscript css-lite cl-json usocket hunchentoot fiveam))

(in-package :cl-user)

(defpackage :euler
 (:use :cl :cl-who :hunchentoot :parenscript)
 (:import-from :css-lite :css)
 (:import-from :json :encode-json-to-string :encode-json))

(in-package :euler)
(load "validation.lisp")
(load "math.lisp")
(load "0001.lisp")

;If the value of this variable is NIL (the default is T), then errors which happen while a request is handled aren't caught as usual, but instead your Lisp's debugger is invoked. This variable should obviously always be set to a true value in a production environment. See MAYBE-INVOKE-DEBUGGER if you want to fine-tune this behaviour.
(setf *catch-errors-p* T)

;; Start our web server.
(setf *web-server* (make-instance 'hunchentoot:acceptor :port 9090 ))
(hunchentoot:start *web-server*) 

;; Publish all static content.
(push (create-static-file-dispatcher-and-handler "/utils.js" "scripts/utils.js") *dispatch-table*)
(push (create-static-file-dispatcher-and-handler "/style.css" "css/retro.css") *dispatch-table*)

;; Automatically creates a Hunchentoot handler for the given URL (plus .htm) associating 
;; it with a function of the same name.
(defmacro define-url-fn ((name) &body body)
  `(progn
     (defun ,name ()
       ,@body)
     (push (create-prefix-dispatcher ,(format nil "/~(~a~)" name) ',name) *dispatch-table*)))

;; All pages on the Retro Games site will use the following macro; less to type and 
;; a uniform look of the pages (defines the header and the stylesheet).
(defmacro standard-page ((&key title) &body body)
  `(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
     (:html :xmlns "http://www.w3.org/1999/xhtml"  :xml\:lang "en" :lang "en"
       (:head 
         (:meta :http-equiv "Content-Type" :content "text/html;charset=utf-8")
	  (:title ,title)
           (:script :type "text/javascript" :src "/utils.js")
           (:script :type "text/javascript" :src "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
	   (:link :type "text/css" :rel "stylesheet" :href "/styles.css")
          (:body 
		     ,@body)))))

;;
;; The functions responsible for generating the HTML go here.
;;

(defun readfile (filename)
(with-open-file (stream filename)
    (do ((line (read-line stream nil)
               (read-line stream nil)))
        ((null line))
      (format t "~a~%" line))))

(define-url-fn (get-euler1)
  (standard-page (:title "Euler Problem 1")
    (:div :id "errorMessage")
    (:p (readfile "templates/euler1.html"))))


(define-url-fn (answer-euler1)
  (encode-json-to-string (euler1 (parse-integer (parameter "maximum")) (string-to-list (parameter "integer-list") ))))

(define-url-fn (checker)
  (encode-json-to-string
    (if (string= (parameter "type") "integer") (checkStringForInteger (parameter "candidate"))
    (if (string= (parameter "type") "listofint") (containsOnlyPositiveIntegers (string-to-list (parameter "candidate")))))))
